package cvo.chess.client.entities.armies.pieces;

import java.util.ArrayList;
import java.util.List;

import cvo.chess.client.entities.IGameFacade;
import cvo.chess.client.entities.chessboard.ChessboardCoordinate;
import cvo.chess.client.entities.chessboard.IChessboardCoordinate;
import cvo.chess.client.entities.game.Army;
import cvo.chess.client.entities.game.Piece;

/**
 * @author Jo
 */
public class King extends Piece {

	public King(IGameFacade gf, Army army) {
		super(gf, army);
	}

	public King(IGameFacade gf, Army army, String name) {
		super(gf, army, name);
	}

	@Override
	public List<ChessboardCoordinate> getPossibleMoves() throws Exception {
		List<ChessboardCoordinate> possibleMoves = new ArrayList<ChessboardCoordinate>();
		IChessboardCoordinate currentPosition = gf.getPositionOfPiece(this);

		ChessboardCoordinate possiblePosition = null;

		Integer row = currentPosition.getRow();
		Integer col = currentPosition.getCol();

		for (int i = -1; i <= 1; i++) {
			if ((row + i) >= 0 && (row + i) < gf.getBoard().getWidth()) {
				for (int a = -1; a <= 1; a++) {
					if ((col + a) >= 0 && (col + a) < gf.getBoard().getHeight()) {
						possiblePosition = new ChessboardCoordinate(
								gf.getBoard(), currentPosition.getCol() + a,
								currentPosition.getRow() + i);
						if (gf.getPieceAt(possiblePosition) == null) {
							possibleMoves.add(possiblePosition);
						}else if(possiblePosition != currentPosition && !gf.getPieceAt(possiblePosition).getColor().equalsIgnoreCase(this.getColor())){
							possibleMoves.add(possiblePosition);
						}
					}
				}
			}
		}
		return possibleMoves;
	}

}
